<?php
/**
 * Created by PhpStorm.
 * User: ZhangWei
 * Date: 2018/1/11
 * Time: 12:04
 */

namespace App\Helpers;


use Illuminate\Support\Facades\Storage;
use zgldh\QiniuStorage\QiniuStorage;

class FileTool
{
    public static function thumb($path, $param = null)
    {
        if (empty($path)) {
            return '';
        }
        if (str_contains($path, ['http://', 'https://'])) {
            if (str_contains($path, ['http://' . config('filesystems.disks.qiniu.domains.default')])) {
                $url = $path . '?&' . $param;
            } else {
                $url = $path;
            }
        } else {
            $url = Storage::imagePreviewUrl($path, $param);
        }
        return $url;
    }

    public static function video($path, $param = null)
    {
        if (empty($path)) {
            return '';
        }

        if (str_contains($path, ['http://', 'https://'])) {
            $url = $path;
        } else {
            $disk = QiniuStorage::disk('qiniu');
            $url = $disk->imagePreviewUrl($path, $param);
        }
        return $url;
    }

    public static function getImageView($w, $h)
    {
        return "imageView2/1/w/$w/h/$h/q/100";
    }


    /**
     * @param $qr_poster
     * @param bool $avatar
     * @param bool $name
     * @param bool $title
     * @param bool $cover
     * @param bool $liveroom
     * @param bool $qr_code
     * @return array
     */

    public static function shareCard($qr_poster, $avatar = false, $name = false, $title = false, $cover = false, $liveroom = false, $start_time = false, $qr_code = false)
    {

        $bg = self::thumb($qr_poster->path);
        $params = $qr_poster->params_data;

        //用户名称水印设置
        $name_w = $name && $params->name ? '/text/'
            . self::base64($name)
            . '/font/' . self::base64($params->name_info->font)
            . '/fontsize/' . $params->name_info->fontsize
            . '/fill/' . self::base64($params->name_info->fill)
            . '/gravity/' . $params->name_info->gravity
            . '/dx/' . $params->name_info->dx
            . '/dy/' . $params->name_info->dy
            : '';
        //标题水印设置
        //$title = $title;//str_limit($title, 69 - (intval($params->title_info->fontsize / 6.8) - 69));
        $title_font_size = $params->title_info->fontsize * 0.05;
        $title_font_width = $params->title_info->w ?? 450;
        $ar = str_split($title, 3);
        $cw = 0;
        $content = "";
        foreach ($ar as $k => $v) {
            $cw += $title_font_size;
            if ($cw > $title_font_width) {
                $content .= $v . "\n";
                $cw = 0;
            } else {
                $content .= $v;
            }

        }
        $title = $content;
        $title_info_w = $title && $params->title ? '/text/'
            . self::base64($title)
            . '/font/' . self::base64($params->title_info->font)
            . '/fontsize/' . $params->title_info->fontsize
            . '/fill/' . self::base64($params->title_info->fill)
            . '/gravity/' . $params->title_info->gravity
            . '/dx/' . $params->title_info->dx
            . '/dy/' . $params->title_info->dy
            : '';
        //直播间名称水印设置
        $liverrom_w = $liveroom && $params->liveroom ? '/text/'
            . self::base64($liveroom)
            . '/font/' . self::base64($params->liveroom_info->font)
            . '/fontsize/' . $params->liveroom_info->fontsize
            . '/fill/' . self::base64($params->liveroom_info->fill)
            . '/gravity/' . $params->liveroom_info->gravity
            . '/dx/' . $params->liveroom_info->dx
            . '/dy/' . $params->liveroom_info->dy
            : '';
        //开课时间水印设置
        $w = date("w", strtotime($start_time));
        $wr = ['星期天', '星期一', '星期二', '星期三', '星期四', '星期五', '星期六',];
        $ww = $wr[$w];
        $start_time_str = date("Y-m-d $ww H:i", strtotime($start_time));
        $start_time_w = $start_time && $params->start_time ? '/text/'
            . self::base64($start_time_str)
            . '/font/' . self::base64($params->start_time_info->font)
            . '/fontsize/' . $params->start_time_info->fontsize
            . '/fill/' . self::base64($params->start_time_info->fill)
            . '/gravity/' . $params->start_time_info->gravity
            . '/dx/' . $params->start_time_info->dx
            . '/dy/' . $params->start_time_info->dy
            : '';
        //头像水印设置
        $avatar_t = self::thumb($avatar, "imageMogr2/auto-orient/thumbnail/!{$params->avatar_info->w}x{$params->avatar_info->h}r/gravity/Center/crop/{$params->avatar_info->w}x{$params->avatar_info->h}/blur/1x0/quality/100");
        if ($params->avatar_info->round) {
            $avatar_t = $avatar_t . "|roundPic/radius/" . $params->avatar_info->w / 2;
        }
        $avatar_w = $avatar && $params->avatar ? '/image/'
            . self::base64($avatar_t)
            . '/dissolve/100'
            . '/gravity/' . $params->avatar_info->gravity
            . '/dx/' . $params->avatar_info->dx
            . '/dy/' . $params->avatar_info->dy
            : '';
        //封面水印设置
        $cover = self::thumb($cover, "imageMogr2/auto-orient/thumbnail/!{$params->cover_info->w}x{$params->cover_info->h}r/gravity/Center/crop/{$params->cover_info->w}x{$params->cover_info->h}/blur/1x0/quality/100");
        $cover_w = $cover && $params->cover ? '/image/'
            . self::base64($cover)
            . '/dissolve/100'
            . '/gravity/' . $params->cover_info->gravity
            . '/dx/' . $params->cover_info->dx
            . '/dy/' . $params->cover_info->dy
            : '';

        //二维码水印设置

        $qr_code_t = self::thumb($qr_code, "imageMogr2/auto-orient/thumbnail/!{$params->qr_code_info->w}x{$params->qr_code_info->h}r/gravity/Center/crop/{$params->qr_code_info->w}x{$params->qr_code_info->h}/blur/1x0/quality/100");
        if ($params->qr_code_info->round) {
            $qr_code_t = $qr_code_t . "|roundPic/radius/" . $params->qr_code_info->w / 2;
        }
        $qr_code_w = $qr_code && $params->qr_code ? '/image/'
            . self::base64($qr_code_t)
            . '/dissolve/100'
            . '/gravity/' . $params->qr_code_info->gravity
            . '/dx/' . $params->qr_code_info->dx
            . '/dy/' . $params->qr_code_info->dy
            : '';
        $sp = $name_w . $title_info_w . $liverrom_w . $cover_w . $qr_code_w . $avatar_w . $start_time_w;

        $s_name = md5($qr_poster->path . $sp);

        $ss = base64_encode(config('filesystems.disks.qiniu.bucket') . ':share_card/' . $s_name);

        $fop = 'watermark/3' . $sp . '|saveas/' . $ss;
        $disk = QiniuStorage::disk('qiniu');

        $path = 'share-card-ls/';

        if (!$disk->exists($path . $s_name)) {
            $persistent_fop_id = $disk->persistentFop($qr_poster->path, $fop, 'fufei-read');
            $data['fop_id'] = $persistent_fop_id;
            $data['card_url'] = FileTool::thumb($qr_poster->path, 'watermark/3' . $sp);
        } else {
            $data['card_url'] = self::thumb($path . $s_name);
        }
        $data['qr_code'] = self::thumb($qr_code);
        //$data['avatar'] = $avatar_t;

        return $data;

    }

    private static function base64($str)
    {
        $ss = base64_encode($str);

        $ss = str_replace("+", "-", $ss);
        $ss = str_replace("/", "_", $ss);

        return $ss;
    }
}